package com.meilishuo.android.performance;

        import org.jfree.chart.ChartFactory;
        import org.jfree.chart.ChartUtilities;
        import org.jfree.chart.title.TextTitle;
        import org.jfree.data.time.*;
        import org.jfree.chart.plot.XYPlot;
        import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
        import java.awt.Color;
        import org.jfree.ui.RectangleInsets;
        import java.awt.Font;
        import org.jfree.chart.renderer.xy.XYItemRenderer;
        import org.jfree.chart.JFreeChart;
        import org.jfree.chart.labels.*;
        import org.jfree.ui.*;
        import java.awt.*;
        import java.io.FileOutputStream;
        import java.io.OutputStream;
        import java.text.*;
        import java.util.*;
        import java.util.List;
        import org.jfree.chart.axis.DateAxis;
        import org.jfree.chart.axis.ValueAxis;
        import org.jfree.chart.axis.NumberAxis;

public class JFreeChartUtil{

    // 生成图表主对象JFreeChart
    public static JFreeChart createChart(TimeSeriesCollection linedataset) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart("访问量统计时间线", "月份",
                "访问量", linedataset, true, true, true);

        // 设置消除字体的锯齿渲染(解决中文问题)
        chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

        // 设置主标题
        chart.setTitle(new TextTitle("blog访问量统计", new Font("楷书", Font.ITALIC,
                20)));
        chart.setAntiAlias(true);
        // 设置子标题
        TextTitle subtitle = new TextTitle("2010/2011年度访问量对比", new Font("宋体",
                Font.BOLD, 12));
        chart.addSubtitle(subtitle);

        XYPlot plot = (XYPlot) chart.getPlot();

        XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) plot
                .getRenderer();

        // 设置网格背景颜色
        plot.setBackgroundPaint(Color.white);
        // 设置网格竖线颜色
        plot.setDomainGridlinePaint(Color.pink);
        // 设置网格横线颜色
        plot.setRangeGridlinePaint(Color.pink);
        // 设置曲线与xy轴的距离
        plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));
        // 设置曲线是否显示数据点
        xylineandshaperenderer.setBaseShapesVisible(true);
        // 设置曲线显示各项数据点的值
        XYItemRenderer xyitem = plot.getRenderer();
        xyitem.setBaseItemLabelsVisible(true);
        xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
        xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
        xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 14));
        plot.setRenderer(xyitem);

        // 设置X轴日期显示的格式
        SimpleDateFormat format = new SimpleDateFormat("h/m/S");
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(format);// 设置x轴数据单位以×年×月方式显示

        ValueAxis valueAxis = plot.getDomainAxis();
        // 设置x轴上面的字体
        valueAxis.setTickLabelFont(new Font("隶书", Font.ITALIC, 12));
        // 设置X轴的标题文字
        valueAxis.setLabelFont(new Font("隶书", Font.ITALIC, 12));
        NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
        // 设置y轴上的字体
        numberAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 13));
        // 设置y轴上的标题字体
        numberAxis.setLabelFont(new Font("宋体", Font.PLAIN, 13));
        // 设置底部的字体
        chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));

        return chart;
    }

    // 生成数据
    public static TimeSeriesCollection createDataset(java.util.List<Double> valueList) {
        Date fromDate = new Date(2015, 1,1,1,1,1);
        SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd EE hh:mm:ss");
        System.out.println(dd.format(new Date(fromDate.getTime() +  1000)));

        // 访问量统计时间线
        TimeSeries timeSeries1 = new TimeSeries("2010年度", Second.class);

        // 时间曲线数据集合
        TimeSeriesCollection lineDataset = new TimeSeriesCollection();

        // 构造数据集合
        for(Double index : valueList)
        {
            Date newDate = new Date((fromDate.getTime()+ 1000));
            timeSeries1.add(new Second(newDate), index);
            fromDate = newDate;
        }

        //timeSeries1.add(new Second(new Date()), 5600);
        lineDataset.addSeries(timeSeries1);

        return lineDataset;
    }

    public static void generateChart(List<Double> valueList, String fileName){
        JFreeChart chart = createChart(createDataset(valueList));
        try {


            OutputStream os = new FileOutputStream(fileName);//图片是文件格式的，故要用到FileOutputStream用来输出。
            ChartUtilities.writeChartAsJPEG(os, chart, 1000, 800);
            //使用一个面向application的工具类，将chart转换成JPEG格式的图片。第3个参数是宽度，第4个参数是高度。

            os.close();//关闭输出流
        } catch (Exception e)
        {
            e.printStackTrace();
        }

    }

}